<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Add extends Controller
{
    public function add()
    {
        require("Redis.php");
        $username = $_POST['username'];
        $email = $_POST['email'];
        $qq = $_POST['qq'];
        $photo = $_POST['photo'];
        $tel = $_POST['tel'];
        $password = $_POST['password'];
        $role = $_POST['role'];
        $uid = $redis->incr("userid");
        $redis->hmset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"tel"=>$tel,"role"=>$role,"email"=>$email,"qq"=>$qq,"photo"=>$photo));
        $redis->rpush("uid",$uid);
        $redis->set("username:".$username,$uid);


        $username = input("param.username");
        $email = input("param.email");
        $qq = input("param.qq");
        $tel = input("param.tel");
        $password = input("param.password");
        $repassword = input("param.repassword");

        if ($password !== $repassword){
            return $this->error('两次密码不一致,请重新填写');
        }
        $password = md5($password);

        // 页面接收的数据(以post方式传输 路由方式为post)
        $p = input('post.');

        // 对数据进行验证, 不符合返回页面
        $check = $this->validate($p, 'index/AddValidate');
        if ($check !== true) {
            $this->error($check);
            return;
        }

        $data = [
            'username' => $username,
            'tel' => $tel,
            'password' => $password,
            'role' =>$role,
        ];

        $result = db()->table('wb_buser')->insertGetId($data);

        $res = [
            'username' =>$username,
            'uid' =>$result,
            'email' =>$email,
            'qq' =>$qq,
            'photo' =>$photo,
        ];

        db()->table('wb_buserinfo')->insertGetId($res);

        $this->success('注册成功','index/login/index');
    }

}

